﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Scoreboard.Model.Authentication;
using Scoreboard.Basement;

namespace Scoreboard.Model
{
	public partial class Participant
	{
		public int GamesNumber { get; set; }
		public double PointsNumber { get; set; }
		public double Score { get; set; }
		public int Place { get; set; }
		public double ScoresDifference { get; set; }
		public bool IsWinner { get; set; }

		public string Name
		{
			get
			{
				if (Mate == null)
				{
					if (Player == null)
					{
						return string.Empty;
					}
					return Player.FriendlyName;
				}
				return string.Format("{0} / {1}", Player.FriendlyName, Mate.FriendlyName);
			}
		}		

		public int Rating
		{	
			get
			{
				if (Mate == null)
				{
					if (Player.RatingGames >= AppSettings.MinGamesForRatingNumber)
					{
						return Player.RatingValue;
					}
					return 0;
				}

				if (Player.RatingGames >= AppSettings.MinGamesForRatingNumber
					&& Mate.RatingGames >= AppSettings.MinGamesForRatingNumber)
				{
					return (Player.RatingValue + Mate.RatingValue) / 2;
				}
				return 0;
			}
		}

		public Participant Copy()
		{
			var participantCopy = new Participant
			{
				Player = this.Player,
				Mate = this.Mate
			};

			return participantCopy;
		}

		public bool BelongsTo(ScoreboardUser user)
		{
			if (Player.Login.ToUpperInvariant() == user.Name.ToUpperInvariant())
			{
				return true;
			}
			if ((Mate != null) && (Mate.Login.ToUpperInvariant() == user.Name.ToUpperInvariant()))
			{
				return true;
			}
			return false;
		}

		public override bool Equals(object obj)
		{
			var anotherParticipant = obj as Participant;
			if (anotherParticipant == null)
			{
				return false;
			}

			return this == anotherParticipant;
		}

		public override int GetHashCode()
		{
			return Name.GetHashCode();
		}

		public static bool operator ==(Participant participant1, Participant participant2)
		{
			if (System.Object.ReferenceEquals(participant1, participant2))
			{
				return true;
			}

			if (((object)participant1 == null) || ((object)participant2 == null))
			{
				return false;
			}

			return (participant1.Player == participant2.Player)
					&& (participant1.Mate == participant2.Mate);
		}

		public static bool operator !=(Participant participant1, Participant participant2)
		{
			return !(participant1 == participant2);
		}
	}
}
